package com.api.shopapi.mapper;

import com.api.shopapi.entity.Berth;
import com.api.shopapi.entity.dto.BerthCountDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author maochuan
 */
@Repository
@Mapper
public interface BerthMapper {
    @Insert("insert into berth " +
            "(BerthCode,RoomCode,RoomNumber,RoomFloor,Number,Status,StaffCode,InstitutionCode) " +
            "value(#{berth.BerthCode},#{berth.RoomCode},#{berth.RoomNumber},#{berth.RoomFloor},#{berth.Number}," +
            "#{berth.Status},#{berth.StaffCode},#{berth.InstitutionCode})")
    int insert(@Param("berth") Berth berth);

    @Delete("delete from berth where berthCode=#{berthCode}")
    int deleteByBerthCode(@Param("berthCode") String berthCode);

    @Update("update berth set RoomCode = #{berth.RoomCode},RoomNumber = #{berth.RoomNumber},RoomFloor = #{berth.RoomFloor},Number=#{berth.Number},Status=#{berth.Status}," +
            "StaffCode=#{berth.StaffCode},InstitutionCode=#{berth.InstitutionCode} " +
            "where BerthCode=#{berth.BerthCode}")
    int updateByBerthCode(@Param("berth") Berth berth);

    @Select("select * from berth")
    List<Berth> getListAll();

    @Select("select * from berth where Number=#{number} and RoomCode=#{roomCode} and InstitutionCode=#{institutionCode}  limit 1")
    Berth getByrNumber(@Param("number") String number,@Param("roomCode") String roomCode,@Param("institutionCode") String institutionCode);

    @Select("select * from berth where InstitutionCode = #{institutionCode} order by Number")
    List<Berth> getListByStaffCode(@Param("institutionCode") String institutionCode);


    //机构床位总数
    @Select("select BerthCode from berth where InstitutionCode = #{institutionCode}")
    List<Berth> ListByInstitutionCode(@Param("institutionCode") String institutionCode);

    //已入住床位总数
    @Select("select BerthCode from berth where InstitutionCode = #{institutionCode} and Status=2")
    List<Berth> ListCheckinByInstitutionCode(@Param("institutionCode") String institutionCode);


    @Select("<script>" +
            "select * from berth " +
            "where InstitutionCode = #{institutionCode}" +
            "<if test ='number !=0'>" +
            "and Number like concat('%',#{number},'%') " +
            "</if>" +
            "<if test ='roomCode !=\"00000000-0000-0000-0000-000000000000\" '> " +
            "and RoomCode = #{roomCode} " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and Status =#{status} " +
            "</if>" +
            "order by RoomFloor,RoomNumber,Number asc" +
            "</script>")
    List<Berth> getPageList(@Param("institutionCode") String institutionCode,@Param("number") int number,@Param("roomCode") String roomCode,@Param("status") int status);

    @Select("select * from berth where BerthCode=#{berthCode} limit 1")
    Berth getByBerthCode(@Param("berthCode") String berthCode);

    @Select("select * from berth where RoomCode = #{roomCode} and status<11 order by Number asc")
    List<Berth> getListByRoomCode(@Param("roomCode") String roomCode);


    @Select("select * from berth where RoomCode = #{roomCode} and Status=0 order by Number asc")
    List<Berth> getListByRoomCodeAndStatus0(@Param("roomCode") String roomCode);

    @Select("select * from berth where RoomCode = #{roomCode} and Status!=11 order by Number asc")
    List<Berth> getListByRoomCodeAndStatusNot11(@Param("roomCode") String roomCode);

    @Select("select * from berth where Number=#{number} and RoomCode=#{roomCode} and InstitutionCode=#{institutionCode}  limit 1")
    Berth getByName(@Param("name") String name,@Param("roomCode") String roomCode,@Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "SELECT count(case when `Status` = 0 then 1 end) leisure,count(case when `Status` = 1 then 1 end) subscribe,count(case when `Status` = 2 then 1 end) checkin FROM berth where 1=1" +
            "<if test ='list.size()>0'>" +
            "and InstitutionCode in "+
            "<foreach collection='list' item='obj' separator=',' open='(' close=')'>" +
            "#{obj}"+
            "</foreach>" +
            "</if>" +
            "</script>")
    BerthCountDTO getBerthNumByIns(@Param("list") List<String> list);



    @Select("<script>" +
            "select * from berth " +
            "where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode}) " +
            " order by Number asc" +
            "</script>")
    List<Berth> listByGovCode(@Param("governmentCode") String governmentCode);


}
